home *** CD-ROM | disk | FTP | other *** search
/ APC & TCP 4 / APC & TCP 4.iso / games / publicdomain / a / attacks / sources / attacks.mod next >
Text File  |  1994-05-14  |  10KB  |  327 lines

  1. MODULE attacks;
  2.  
  3. (*   This is the main module for the program Attacks.  It sets up the     *)
  4. (* game, cleans up, and holds the main loop of the game.  There are a few *)
  5. (* little routines in here to assist the main, but most of the routines   *)
  6. (* are located in their respective modules.  Everything in here is writ-  *)
  7. (* ten by me, except the menu module, which was written by Chris Mlsna    *)
  8. (* and modified by me.  The menu module is probably the most useful for   *)
  9. (* applying to your own programs, since its routines are not specifically *)
  10. (* made for this program.  But don't blame me about Chris's lack of docu- *)
  11. (* mentation!                                                             *)
  12. (*                                                                        *)
  13. (*   The whole thing was made using Avant-Garde's Benchmark Modula-2 by   *)
  14. (* by Leon Frenkel.  As you can see, it actually does the job.            *)
  15. (*                                                                        *)
  16. (*   If you have any questions about this program or programming in gene- *)
  17. (* ral, please feel free to contact me.  My address can be found in the   *)
  18. (* program and in the doc file.  Hope you enjoy/learn/whatever!           *)
  19. (*                                                                        *)
  20. (*      Scott Biggs                                                       *)
  21.  
  22.  
  23. (*$R-V-*) (* range checking OFF, overflow checking OFF *)
  24.  
  25. (****************************************)
  26. (*      IMPORTS                         *)
  27. (****************************************)
  28.  
  29. FROM header
  30.   IMPORT   state, squaretype, boardsize, boardtype, movetype, boardrange,
  31.            playertype, gameover, pointercode, currentpointer, difficulty,
  32.            thinkertype, whoisred, whoisblue, printmsgtype, backedup;
  33. FROM attacksgraphics
  34.   IMPORT   InitializeScreen, DrawBoard, DrawSquare, CleanupGraphics,
  35.            ChangePointer, ShowScore, PrintTurn, PrintMsg, ShowAbout;
  36. FROM input
  37.   IMPORT   moveAttempted, eventtype, GetEvent, InitMenus, CloseMenus,
  38.            EditBoard, ChangeToEditMenu, ChangeToMainMenu,
  39.            ChangeToComputerMenu;
  40. FROM boards
  41.   IMPORT   numboards, setup0, setup1, setup2, setup3, setup4;
  42. FROM history
  43.   IMPORT   InitHistory, PopHistory, CloseHistory, AddToHistory, NewHistory,
  44.            UpHistory;
  45. FROM thinker
  46.   IMPORT   LegalMove, GoodMovePossible, DoMove, DoComputerMove;
  47. FROM mdgenerallib
  48.   IMPORT   MyPause;
  49. FROM AmigaDOS
  50.   IMPORT   DateStampRecord, DateStamp;
  51. FROM RandomNumbers
  52.   IMPORT   Seed, Random;
  53. FROM TermInOut
  54.   IMPORT   WriteLn, WriteString;
  55.  
  56.  
  57. (************************************************************************)
  58.  
  59. PROCEDURE NewBoard (VAR newboard : boardtype);
  60.  
  61. (*   This chooses one of the available new boards at random and then   *)
  62. (* makes the board that which was chosen.  It's up to the caller to do *)
  63. (* anything with the new board variable.                               *)
  64.  
  65. VAR
  66.   whichboard : CARDINAL;
  67.  
  68. BEGIN
  69.   whichboard := CARDINAL (Random(numboards));
  70.   INC(whichboard);
  71.   CASE whichboard OF
  72.      1  :  newboard := setup1;  |
  73.      2  :  newboard := setup2;  |
  74.      3  :  newboard := setup3;  |
  75.      4  :  newboard := setup4;  |
  76.      ELSE newboard := setup0;
  77.      END;
  78. END NewBoard;
  79.  
  80. (************************************************************************)
  81.  
  82. PROCEDURE setup;
  83.  
  84. VAR
  85.   d : DateStampRecord;
  86.  
  87. BEGIN
  88.   DateStamp(d);        (* init random generator   *)
  89.   Seed := LONGCARD(d.dsDays * d.dsMinute * d.dsTick);
  90.  
  91.   IF NOT InitializeScreen() THEN               (* Sets up the graphix   *)
  92.      WriteString("Couldn't initialize graphics.\n");
  93.      HALT;
  94.      END;
  95.   difficulty := 2;        (* makes it kinda' good *)
  96.   whoisred := human;
  97.   whoisblue := computer;
  98.   InitMenus;
  99.   NewBoard(state.board);
  100.   state.turn := red;
  101.   ChangePointer (RedPointer);
  102.   PrintTurn(red);
  103.   currentpointer := RedPointer;
  104.   DrawBoard(state.board);       (* Draws the board on the screen          *)
  105.   IF NOT InitHistory(state.history, state.board, state.turn) THEN
  106.      WriteString("Couldn't initialize the history.\n");
  107.      HALT;
  108.      END;
  109. END setup;
  110.  
  111.  
  112. (************************************************************************)
  113. PROCEDURE Cleanup;
  114. BEGIN
  115.   CloseHistory(state.history);
  116.   ChangePointer(DefaultPointer);
  117.   currentpointer := DefaultPointer;
  118.   CloseMenus;
  119.   CleanupGraphics;
  120. END Cleanup;
  121.  
  122.  
  123. (************************************************************************)
  124. PROCEDURE CheckForGameOver;
  125.  
  126. (*   This checks to see if only one player is remaining.  If so, then  *)
  127. (* gameover is set to TRUE.                                            *)
  128.  
  129. VAR
  130.   i, j : boardrange;
  131.   any : BOOLEAN;
  132.  
  133. BEGIN
  134.   any := FALSE;
  135.   FOR i := 1 TO 7 DO  FOR j := 1 TO 7 DO       (* check for no reds *)
  136.      IF state.board[i,j] = red THEN
  137.         any := TRUE;
  138.         END;
  139.      END; END;
  140.   IF any = FALSE THEN
  141.      gameover := TRUE;
  142.      RETURN;
  143.      END;
  144.  
  145.   any := FALSE;
  146.   FOR i := 1 TO 7 DO  FOR j := 1 TO 7 DO       (* check for no blues *)
  147.      IF state.board[i,j] = blue THEN
  148.         any := TRUE;
  149.         END;
  150.      END; END;
  151.   IF any = FALSE THEN
  152.      gameover := TRUE;
  153.      END;
  154. END CheckForGameOver;
  155.  
  156.  
  157. (************************************************************************)
  158. (*      MAIN                                                            *)
  159. (************************************************************************)
  160.  
  161. VAR
  162. event : eventtype;              (* Holds the code for the input events  *)
  163. quit,                           (* True when the loop should be exited  *)
  164. playmore : BOOLEAN;             (* Is FALSE when the game is over       *)
  165. redscore, bluescore : CARDINAL; (* The respective scores.               *)
  166. i, j : boardrange;
  167. foo : BOOLEAN;                  (* used for scratch *)
  168. fooboard : boardtype;
  169. footurn : playertype;
  170. otherplayer : playertype;
  171.  
  172.  
  173. BEGIN
  174.   setup;
  175.   quit := FALSE;
  176.   gameover := FALSE;
  177.   backedup := FALSE;
  178.  
  179. REPEAT
  180.   redscore := 0;    bluescore := 0;      (* doing the scores *)
  181.   FOR i := 1 TO 7 DO
  182.      FOR j := 1 TO 7 DO
  183.         IF state.board[i,j] = red THEN
  184.            INC(redscore);
  185.            ELSE IF state.board[i,j] = blue THEN
  186.                  INC(bluescore);
  187.                  END;
  188.            END;
  189.         END;
  190.      END;
  191.   ShowScore(redscore, bluescore);
  192.  
  193.            (******************************)
  194.            (* Check & DO computer's turn *)
  195.            (******************************)
  196.  
  197.   IF (NOT gameover) AND
  198.      (NOT backedup) AND
  199.      (((state.turn = red) AND (whoisred = computer)) OR
  200.      ((state.turn = blue) AND (whoisblue = computer)))
  201.   THEN
  202.      PrintMsg(Thinking);
  203.      ChangeToComputerMenu;
  204.      foo := DoComputerMove();
  205.      IF NOT backedup THEN
  206.         foo := AddToHistory(state.history, state.board, state.turn);
  207.         END;
  208.      ChangeToMainMenu;
  209.  
  210.  
  211.            (**************************************)
  212.            (* otherwise, get an event and do it! *)
  213.            (**************************************)
  214.   ELSE
  215.      event := GetEvent(state.turn);
  216.      IF state.turn = red THEN
  217.         otherplayer := blue;
  218.         ELSE otherplayer := red;
  219.         END;
  220.  
  221.      CASE event OF
  222.  
  223.         NEWGAME  :
  224.            gameover := FALSE;
  225.            backedup := FALSE;
  226.            NewBoard(state.board);
  227.            state.turn := red;
  228.            DrawBoard(state.board);
  229.            NewHistory(state.history, state.board, state.turn);
  230.            |
  231.  
  232.         EDIT     :
  233.            ChangeToEditMenu;
  234.            IF EditBoard(state.board, state.turn) THEN
  235.               foo := AddToHistory(state.history, state.board, state.turn);
  236.               IF GoodMovePossible(state.board, state.turn) THEN
  237.                  gameover := FALSE;
  238.                  ELSE
  239.                     IF state.turn = red THEN
  240.                        otherplayer := blue;
  241.                        ELSE otherplayer := red;
  242.                        END;
  243.                     IF GoodMovePossible(state.board, otherplayer) THEN
  244.                        state.turn := otherplayer;
  245.                        gameover := FALSE;
  246.                        ELSE gameover := TRUE;
  247.                        END;
  248.                  END;
  249.               END;
  250.            ChangeToMainMenu;
  251.            |
  252.  
  253.         QUIT     :
  254.            quit := TRUE;